import requests
from xml.etree import ElementTree as ET

# RSS订阅的URL
rss_url = 'http://arxiv.org/rss/physics'

# 发送GET请求获取RSS内容
response = requests.get(rss_url)

# 检查请求是否成功
if response.status_code == 200:
    # 解析XML内容
    tree = ET.fromstring(response.content)

    # RSS命名空间
    ns = {'atom': 'http://www.w3.org/2005/Atom'}

    # 遍历所有item元素
    temp_file = "data/all-articles.md"
    with open(temp_file, "w") as fp:
        pass

    for item in tree.findall('channel/item'):
        # 获取标题
        title = item.find('title').text
        # 获取摘要
        summary = item.find('description').text

        # 获取链接
        link = item.find('link').text

        # 打印标题和摘要
        with open(temp_file, "a") as fp:
            print(f'#### {title}', file=fp)
            new_summary = summary.replace('\n', '\n\n')
            print(f'Summary:{new_summary}', file=fp)
            print(file=fp)
            print(f'Link: {link}', file=fp)
        # print(f'Title: {title}')
        # print(f'Summary: {summary}')
        # print(f'Link: {link}')
        # print('-' * 80)  # 分隔线
else:
    print(f'Failed to retrieve RSS feed: Status code {response.status_code}')
